package library

import (
	"sync"
	"syproxy/service"
)

type Container struct {
	connList map[string]*service.Service
	lock     *sync.Mutex
}

// NewContainer 新建Container
func NewContainer() *Container {
	c := new(Container)
	c.lock = &sync.Mutex{}
	c.connList = make(map[string]*service.Service, 10)

	return c
}

func (c *Container) Push(str string, s *service.Service) {
	c.lock.Lock()
	defer c.lock.Unlock()
	c.connList[str] = s
}

func (c *Container) GetService(str string) *service.Service {
	c.lock.Lock()
	defer c.lock.Unlock()
	return c.connList[str]
}

func (c *Container) Remove(str string) {
	c.lock.Lock()
	defer c.lock.Unlock()
	delete(c.connList, str)
}

func (c *Container) RemoveAll() {
	c.lock.Lock()
	defer c.lock.Unlock()
	for key := range c.connList {
		delete(c.connList, key)
	}
}
